package com.aishua.fire.jpa;

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import com.aishua.fire.jpa.entity.Accountteam;

public interface AccountteamDao extends JpaRepository<Accountteam, Long>,JpaSpecificationExecutor<Accountteam> {
	
	@Transactional
	Long deleteByAccountteamNameAndAccountteamDesc(String accountteamName,String accountteamDesc);

//	@NamedQuery(query="delete from accountteam where accountteam_name = ?1 and accountteam_desc = ?2")
//	long deleteTest(String name,String desc);
	
	 @Transactional
	 @Query("select u from Accountteam u where u.accountteamName = ?1")
	 Accountteam findByAccountteamName(String accountteamName);
	
	 @Transactional
	 @Modifying
	 @Query(value = "SELECT ateam FROM Accountteam ateam " + 
	 		"	, AccountteamPermteam apteam" + 
	 		"	, Permteam pteam " + 
	 		"	, PermPermteam ppteam " + 
	 		"	, Perm p "
	 		+ " where  apteam.accountteamPermteamId.accountteamId = ateam.accountteamId "
	 		+ " and pteam.permteamId =  apteam.accountteamPermteamId.permteamId and apteam.authType = ?3"
	 		+ " and ppteam.permpermteamId.permteamId = pteam.permteamId "
	 		+ " and p.permId = ppteam.permpermteamId.permId "
	 		+ " and p.permId = ?1 and p.type = ?2")
	 List<Accountteam> findAteamsWithUrlByPermId(long permId, String type,String authType);

	 @Transactional
	 @Modifying
	 @Query(value = "SELECT ateam FROM Accountteam ateam " + 
	 		"	, AccountteamPermteam apteam" + 
	 		"	, Permteam pteam " + 
	 		"	, PermPermteam ppteam " + 
	 		"	, Perm p "
	 		+ " where  apteam.accountteamPermteamId.accountteamId = ateam.accountteamId "
	 		+ " and pteam.permteamId =  apteam.accountteamPermteamId.permteamId "
	 		+ " and ppteam.permpermteamId.permteamId = pteam.permteamId "
	 		+ " and p.permId = ppteam.permpermteamId.permId "
	 		+ " and p.permId = ?1 and p.type = ?2")
	 List<Accountteam> findAteamsWithDBByPermId(long permId, String type);
	 
	 /**
	  * 通过用户名
	  * @param userName
	  * @return
	  */
	 @Transactional
	 @Modifying
	 @Query(value = "SELECT ateam FROM Accountteam ateam " + 
			"	, AccountAccountteam aateam" +
	 		"	, Account account" 
	 		+ " where  aateam.accountAccountteamId.accountteamId = ateam.accountteamId "
	 		+ " and account.loginId =  aateam.accountAccountteamId.accountId "
	 		+ " and account.userName = ?1 and account.status=0")
	 List<Accountteam> findByAccountName(String userName);

	 /**
	  * 通过账户组id找到对应的账户信息
	  * @param userName
	  * @return
	  *//*
	 @Transactional
	 @Modifying
	 @Query(value = "SELECT account FROM Accountteam ateam " + 
			"	, AccountAccountteam aateam" +
	 		"	, Account account" 
	 		+ " where  aateam.accountAccountteamId.accountteamId = ateam.accountteamId "
	 		+ " and account.loginId =  aateam.accountAccountteamId.accountId "
	 		+ " and ateam.accountteamId = ?1 and account.status=0")
	 List<Account> findAccountByaccountteamId(Long accountteamId);*/
	 
	 /**
	  * 通过账户id找到对应的账户组信息
	  * @param accountId
	  * @return
	  */
	 @Transactional
	 @Modifying
	 @Query(value = "SELECT b.* FROM accountteam b,account_accountteam ab WHERE b.accountteam_id=ab.accountteam_id" + 
	 		"			  AND ab.`account_id`=?1   ",nativeQuery=true)
	 List<Accountteam> findByaccountId(Long accountId);
	 
	 @Query(value="select * from accountteam",nativeQuery = true)
		List<Accountteam> getAccList();

	 @Transactional
	 @Modifying
	 @Query(value = "SELECT ateam FROM Accountteam ateam " + 
	 		"	, AccountteamPermteam apteam" + 
	 		"	, Permteam pteam " + 
	 		"	, PermPermteam ppteam " + 
	 		"	, Perm p "
	 		+ " where  apteam.accountteamPermteamId.accountteamId = ateam.accountteamId "
	 		+ " and pteam.permteamId =  apteam.accountteamPermteamId.permteamId "
	 		+ " and ppteam.permpermteamId.permteamId = pteam.permteamId "
	 		+ " and p.permId = ppteam.permpermteamId.permId "
	 		+ " and p.permId = ?1 and p.type = ?2 and (apteam.authAction = ?3 or apteam.authAction = ?4)")
	 List<Accountteam> findAteamsWithDBByPermId(long permId, String type,String authAction,String alwaysKey);
	 
	
	
}
